/**
 * @author zhuo
 */

import pathSimple from "../path/simple";
import DataSet from "../../Hmap/DataSet";

export default {
  draw: function(context, dataSet, options) {
    // 获取数据
    var data = dataSet instanceof DataSet ? dataSet.get() : dataSet;
    // console.log('xxxx',options)
    context.save();
    // 遍历配置项属性，设置绘图属性
    for (var key in options) {
      context[key] = options[key];
    }
    // 遍及数据
    for (var i = 0, len = data.length; i < len; i++) {
      var item = data[i];

      context.save();
      // 如果数据项中有绘图颜色，则配置绘图颜色
      if (item.fillStyle || item._fillStyle) {
        context.fillStyle = item.fillStyle || item._fillStyle;
      }
      // 描边颜色
      if (item.strokeStyle || item._strokeStyle) {
        context.strokeStyle = item.strokeStyle || item._strokeStyle;
      }

      if (context.lineDash) {
        context.setLineDash(context.lineDash);
      }

      if (item.lineDash) {
        context.setLineDash(item.lineDash);
      }
      // 绘图类型
      var type = item.geometry.type;

      context.beginPath();

      options.multiPolygonDraw = function() {
        context.fill();

        if ((item.strokeStyle || options.strokeStyle) && options.lineWidth) {
          context.stroke();
        }
      };
      // 绘制形状
      pathSimple.draw(context, item, options);

      if (type == "Point" || type == "Polygon" || type == "MultiPolygon") {
        context.fill();
        if ((item.strokeStyle || options.strokeStyle) && options.lineWidth) {
          context.stroke();
        }
      } else if (type == "LineString" || type == "MultiLineString") {
        if (item.lineWidth || item._lineWidth) {
          context.lineWidth = item.lineWidth || item._lineWidth;
        }
        context.stroke();
      }

      context.restore();
    }

    context.restore();
  }
};
